package com.onlineMIS.ORM.DAO.headQ.supplier.supplierMgmt;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQEntityTagDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagGroupDaoImpl;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagGroup;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadqEntityTag;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCust;
import com.onlineMIS.ORM.entity.headQ.supplier.supplierMgmt.HeadQSupplier;
import com.onlineMIS.common.Common_util;


@Repository
public class HeadQSupplierDaoImpl extends BaseDAO<HeadQSupplier>{
	
	@Autowired
	private HeadQEntityTagDaoImpl headQEntityTagDaoImpl;
	@Autowired
	private HeadQTagDaoImpl headQTagDaoImpl;
	@Autowired
	private HeadQTagGroupDaoImpl headQTagGroupDaoImpl;
	
	
	/**
	 * 生成一个所有供应商的对象	
	 * @return
	 */
	public static HeadQSupplier getAllSupplierObj() {
		HeadQSupplier supplier = new HeadQSupplier();
		supplier.setId(0);
		supplier.setName("所有供应商");
		return supplier;
	}
	
	/**
	 * 获取所有还在运营供应商的client id
	 * @return
	 */
	public Set<Integer> getAllSupplierIds(){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQSupplier.class);
		criteria.add(Restrictions.ne("status", HeadQSupplier.CustStatusEnum.DELETED.getKey()));
		List<HeadQSupplier> suppliers = getByCritera(criteria, true);
		Set<Integer> supplierIds = new HashSet<Integer>();
		for (HeadQSupplier store : suppliers){
			supplierIds.add(store.getId());
		}
		return supplierIds;
	}

	
	/**
	 * 获取所有供应商信息
	 * @return
	 */
	public List<HeadQSupplier> getAllSuppliers() {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQSupplier.class);
		criteria.add(Restrictions.ne("status", HeadQSupplier.CustStatusEnum.DELETED.getKey()));
		return getByCritera(criteria, true);
	}
	
	
	/**
	 * 查询供应商tag标签集合
	 * @return
	 */
	public List<Integer> getSupplierTags() {
		List<Integer> list = headQTagDaoImpl.getAllTagIds();
		return list;
	}
	

	/**
	 * 批量添加标签
	 * 
	 * @param ids
	 * @param tagId
	 * @param type
	 * @return
	 */
	public Response saveBatchSupplierTags(int[] ids, int tagId) {
		Response response = new Response();
		HeadQTag headQTag = headQTagDaoImpl.get(tagId, true);
		// 根据分组id获取分组对象
		HeadQTagGroup hGroup = headQTagGroupDaoImpl.get(headQTag.getGroupId().getId(), true);
		if (ids.length > 0) {
			// 校验所有供应商
			for (int entityId : ids) {
				HeadQSupplier cust = this.get(entityId, true);

				HeadqEntityTag entityTag = headQEntityTagDaoImpl.selectEntityTag(entityId, tagId);
				if (entityTag != null) {
					response.setSuccess("供应商: " + cust.getName() + "已有 " + hGroup.getGroupName() + "标签,请勿重复添加!");
					response.setReturnCode(Response.SUCCESS);
					return response;
				} else {
					// 校验供应商标签
					Boolean result = headQEntityTagDaoImpl.isExistence(entityId, tagId);
					if (result) {
						response.setSuccess("供应商: " + cust.getName() + "  已有" + hGroup.getGroupName() + "标签,请勿重复添加!");
						response.setReturnCode(Response.SUCCESS);
						return response;
					} else {
						break;
					}
				}
			}

		}
		// 批量添加供应商标签
		this.saveBeachEntity(ids, tagId);
		response.setSuccess("供应商标签添加成功!");
		response.setReturnCode(Response.SUCCESS);
		return response;
	}

	/**
	 * 批量添加供应商标签
	 * 
	 * @param ids
	 * @param tagId
	 */
	private void saveBeachEntity(int[] ids, int tagId) {
		if (ids.length != 0) {
			for (int entityId : ids) {
				HeadqEntityTag entity = new HeadqEntityTag();
				entity.setEntityId(entityId);
				entity.setTagId(tagId);
				headQEntityTagDaoImpl.save(entity, true);
			}
		}

	}

	/**
	 * 批量删除供应商标签
	 * 
	 * @param ids
	 * @param tagId
	 */
	private void deleteBeachEntity(int[] ids, int tagId) {
		if (ids.length != 0) {
			for (int entityId : ids) {
				HeadqEntityTag entity = headQEntityTagDaoImpl.selectEntityTag(entityId, tagId);
				headQEntityTagDaoImpl.delete(entity, true);
			}
		}

	}

	/**
	 * 批量删除供应商标签
	 * 
	 * @param ids
	 * @param tagId
	 * @return
	 */
	public Response deleteBatchSupplierTags(int[] ids, int tagId) {
		Response response = new Response();
		if (ids.length > 0) {
			// 校验所有供应商
			for (int entityId : ids) {
				HeadQSupplier cust = this.get(entityId, true);
				HeadQTag headQTag = headQTagDaoImpl.get(tagId, true);
				HeadqEntityTag entityTag = headQEntityTagDaoImpl.selectEntityTag(entityId, tagId);
				if (entityTag == null) {
					response.setSuccess(
							"标签删除失败, 供应商: " + cust.getName() + "未包含:【 " + headQTag.getTagName() + " 】标签信息!");
					response.setReturnCode(Response.SUCCESS);
					return response;
				}
			}
			// 批量删除供应商标签供应商
			this.deleteBeachEntity(ids, tagId);
			response.setSuccess("供应商标签删除成功!");
			response.setReturnCode(Response.SUCCESS);
		}
		return response;
	}

	/**
	 * 临时使用：一键添加禧乐仓供应商标签
	 * 
	 * @return
	 */

	public Response oneKeyBatchInsertSupplieTags() {
		Response response = new Response();
		// 禧乐仓标签
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQSupplier.class);
		criteria.add(Restrictions.ne("status", HeadQSupplier.CustStatusEnum.DELETED.getKey()));
		criteria.add(Restrictions.like("name", "禧-", MatchMode.START));
		List<HeadQSupplier> suppliers = getByCritera(criteria, true);
		if (suppliers.size() != 0) {
			for (HeadQSupplier headQSupplier : suppliers) {
				HeadqEntityTag etag = headQEntityTagDaoImpl.selectEntityTag(headQSupplier.getId(), 23);
				if (etag != null) {
					continue;
				} else {
					HeadqEntityTag entity = new HeadqEntityTag();
					entity.setEntityId(headQSupplier.getId());
					entity.setTagId(23);// 23为禧乐仓供应商标签id
					headQEntityTagDaoImpl.save(entity, true);
				}
			}
		}

		// 千禧标签
		DetachedCriteria criteria2 = DetachedCriteria.forClass(HeadQSupplier.class);
		criteria2.add(Restrictions.ne("status", HeadQSupplier.CustStatusEnum.DELETED.getKey()));
		criteria2.add(Restrictions.not(Restrictions.like("name", "禧-", MatchMode.START)));
		List<HeadQSupplier> qxSuppliers = getByCritera(criteria2, true);
		if (suppliers.size() != 0) {
			for (HeadQSupplier headQSupplier : qxSuppliers) {
				HeadqEntityTag etag = headQEntityTagDaoImpl.selectEntityTag(headQSupplier.getId(), 20);
				if (etag != null) {
					continue;
				} else {
					HeadqEntityTag entity = new HeadqEntityTag();
					entity.setEntityId(headQSupplier.getId());
					entity.setTagId(20);// 20为千禧供应商标签id
					headQEntityTagDaoImpl.save(entity, true);
				}
			}
		}
		response.setSuccess("一键添加标签成功");
		return response;
	}

	/**
	 * 根据供应商ID集合，获取供应商期初金额合计
	 * @param supplierIds
	 * @return
	 */
	public double selectInitialAcctByIds(List<Integer> supplierIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQSupplier.class);	
		criteria.add(Restrictions.ne("status", HeadQSupplier.CustStatusEnum.DELETED.getKey()));

		criteria.add(Restrictions.in("id", supplierIds));
		
		
		criteria.setProjection(Projections.sum("initialAcctBalance"));
		
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionDoubleValue(result);
	}
		
}
